
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
//element-plus
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import myCookies from './utils/myCookies'
import { transformRoutes } from './utils/routeUtils'
import { Icon } from '@iconify/vue';
import DataVVue3 from '@kjgl77/datav-vue3'
const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}






//添加前置路由导航守卫
router.beforeEach((to, from, next) => {
  const hasToken = myCookies.getToken()
  // 定义无需认证的路由
  const whiteList = ['/login', '/register']
  if (whiteList.includes(to.path)) {

    next()
  } else if (!hasToken) {
    // 未登录且不在白名单，跳转登录
    next('/login')
  }
  else {    // 已登录用户正常访问

    next()
  }
})
try {
  const Menus = JSON.parse(localStorage.getItem("menu") || "");
  if (Menus !== "" || Menus !== undefined || Menus !== null) {
    const dynamicRoutes = transformRoutes(Menus);
    // 添加动态路由
    dynamicRoutes.forEach((route) => {
      router.addRoute("layout", route);
    });
  }

} catch (E) {
  console.log(E);
}

app.use(DataVVue3)
app.component("Icon", Icon)
app.use(createPinia())
app.use(router)
app.use(ElementPlus, {
  locale: zhCn
})
app.mount('#app')
function getRoutes(): { data: any } | PromiseLike<{ data: any }> {
  throw new Error('Function not implemented.')
}

